/**
 * Created by zx on 2017/3/10.
 */



function list(maxItems,itemsPerLoad,b){
  // 加载flag
  var loading = false;
  // 最多可加载的条目
  var maxItems = maxItems;

  // 每次加载添加多少条目
  // var itemsPerLoad = itemsPerLoad;
  if(maxItems > itemsPerLoad){
    $('.infinite-scroll-bottom .list-container ul').after('<div class="infinite-scroll-preloader"><div class="preloader"></div></div>');
  }
  function addItems(itemsPerLoad, lastIndex) {
    var j = (lastIndex < maxItems && itemsPerLoad <= maxItems - lastIndex) ? lastIndex + itemsPerLoad : maxItems;
    console.log(j,"j");
    // 生成新条目的HTML
    var html = '';
    for (var i = lastIndex + 1; i <= j; i++) {
      // html += '<div class="card"><div class="card-content"><div class="list-block media-list"><ul><li class="item-content">' +
      //   '<div class="item-media"><img src="http://gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i3/TB10LfcHFXXXXXKXpXXXXXXXXXX_!!0-item_pic.jpg_250x250q60.jpg" width="44"></div>' +
      //   '<div class="item-inner elip"><div class="item-title-row">' +
      //   '<div class="item-title elip">A2 Platinum Infant Formula Stage 1</div></div>' +
      //   '<div class="item-subtitle elip">白金系列1段婴儿配方奶粉</div></div></li></ul></div></div>' +
      //   '<div class="card-footer"><span>900G</span><span style="color:#CC3300;">￥66' + i + '</span></div></div>'
      html += '<li class="swipeout" style="border-bottom:1px solid #e7e7e7;"><div class="swipeout-content">' +
        '<div class="card" style="margin: 0;border-radius:0;"><div class="card-content">' +
        '<div class="list-block media-list"><div class="item-content"><div class="item-media">' +
        '<img src="http://gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i3/TB10LfcHFXXXXXKXpXXXXXXXXXX_!!0-item_pic.jpg_250x250q60.jpg" width="44"></div>' +
        '<div class="item-inner elip" style="border:none;"><div class="item-title-row">' +
        '<div class="item-title elip">A2 Platinum Infant Formula Stage 1</div></div>' +
        '<div class="item-subtitle elip">白金系列1段婴儿配方奶粉</div></div></div></div></div>' +
        '<div class="card-footer" style="border:none;border-radius:0;padding-top: 0;min-height: 1rem;">' +
        '<span>900G</span>' +
        '<span style="color:#CC3300;font-size: .85rem;">￥66' + i + '</span></div></div></div>' +
        '<div class="swipeout-actions-right">' +
        (b ? '<a href="#" style="background: #f60;">加入购物车</a>': '<a href="#" style="background: #CC3300;">删除</a>')+
        '</div></li>';
    }
    // 添加新条目
    $('.infinite-scroll-bottom .list-container ul').append(html);
    $('.infinite-scroll-preloader').css('opacity','0');
  }
  //预先加载多少条
  addItems(itemsPerLoad, 0);
  $('.swipeout:eq(0)').css('border-top','1px solid #e7e7e7');

  // 上次加载的序号

  var lastIndex = itemsPerLoad;

  // 注册'infinite'事件处理函数
  $(document).on('infinite', '.infinite-scroll-bottom',function() {
    $('.infinite-scroll-preloader').css('opacity','1');

    // 如果正在加载，则退出
    if (loading) return;

    // 设置flag
    loading = true;

    // 模拟1s的加载过程
    setTimeout(function() {
      // 重置加载flag
      loading = false;

      if (lastIndex >= maxItems) {
        // 加载完毕，则注销无限加载事件，以防不必要的加载
        $.detachInfiniteScroll($('.infinite-scroll'));
        // 删除加载提示符
        $('.infinite-scroll-preloader').remove();
        $.toast("没有更多商品");
        return;
      }

      // 添加新条目
      addItems(itemsPerLoad, lastIndex);
      // 更新最后加载的序号
      lastIndex = $('.list-container li.swipeout').length;
      // console.log(lastIndex);
      //容器发生改变,如果是js滚动，需要刷新滚动
//          $.refreshScroller();
    }, 1000);
  });

  $(document).on('click','.card',function(){
    window.location.href = 'detail';
  });
}
